<?php
namespace app\controllers;

use app\components\Check;
use app\models\MaterialborrowModel;
use app\models\MaterialModel;
use app\models\ClassroomuseModel;
use app\models\ClassroomModel;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

class ApplyController extends \yii\web\Controller {	
	public $layout = 'front';
	
	/**
	 * @inheritdoc
	 */
	public function behaviors()
	{
		return [
				'access' => [
						'class' => AccessControl::className(),
						'only' => ['index','logout'],
						'rules' => [
								[
										'actions' => ['index','logout'],
										'allow' => true,
										'roles' => ['manager'],
								],
						],
				],
				'verbs' => [
						'class' => VerbFilter::className(),
						'actions' => [
								'delete' => ['POST'],
						],
				],
		];
	}
	
	public  function actionChoose(){
		if (Check::check ()) {
		} else {
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
		}
		return $this->render('choose');
	}
	
	//筛选可用时间段
	public function actionChoosemoment($myRoom,$myDate){
		if (Check::check ()) {
		} else {
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
		}
		
		$classroomuse=new ClassroomuseModel();
		$moments=$classroomuse->find()
							->select(['moment','approved'])
							->where(['classroom'=>$myRoom])
							->andWhere(['time'=>$myDate])
							->asArray()->all();

		$res=json_encode($moments);
		return $res;	
	}
	
	/**
	 * 教室使用-首页
	 */
	public function actionClassroomindex() {
		if (Check::check ()) {
			$stuid = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			return $this->render ( 'classroomindex', [
					'stuid' => $stuid
			] );
		} else {
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
		}
	}
	
	/**
	 * 学生-点击（申请）-创建教室申请记录
	 */
	public function actionClassroomcreate($myRoom,$myDate,$myMoment)
	{
		if (Check::check ()) {			
		} else {
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
		}
		$model = new ClassroomuseModel();	
		
		$count=$model->find()->where(['classroom'=>$myRoom])->andWhere(['time'=>$myDate])->andWhere(['moment'=>$myMoment])->count();
		if($count){
			return $this->render('warning');
		}
		
		if ($model->load(\Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['classroomview', 'id' => $model->id]);
		} else {
			return $this->render('classroomcreate', [
					'model' => $model,
					'classroom'=>$myRoom,
					'time'=>$myDate,
					'moment'=>$myMoment
					
			]);
		}
	}
	
	/**
	 * 学生界面-教室使用-修改按钮
	 */
	public function actionClassroomupdate($id){
		if (Check::check ()) {
		} else {
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
		}
		$model = ClassroomuseModel::findOne($id);
		$myRoom=$model->classroom;
		$myDate=$model->time;
		$myMoment=$model->moment;
		
		if ($model->load(\Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['classroomview', 'id' => $model->id]);
		} else {
			return $this->render ( 'classroomform', [
					'model' => $model,
					'classroom'=>$myRoom,
					'time'=>$myDate,
					'moment'=>$myMoment
			] );
		}	
	}
	/**
	 * 学生-教室使用-详情页
	 */
	public function actionClassroomview($id){
        if (Check::check ()) {
        } else {
            return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
        }
		$model=ClassroomuseModel::findOne($id);
		return $this->render('classroomview', [
				'model' => $model,
		]);
	}
	
	//物资借用
	public function actionMaterial() {
		if (Check::check ()) {
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			return $this->render ( 'material', [
					'id' => $id
			] );
		} else {
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
		}
	}
	/**
	 * 学生-添加（申请）-物资租用记录
	 */
	public function actionCreatematerial()
	{
		if (Check::check ()) {
			$stu_id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			$stu_name=\Yii::$app->session->get ( 'stu' ) ['stuname'];
		} else {
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
		}
		
		$model = new MaterialborrowModel();
		$materials=new MaterialModel();
		$materials=$materials->getAllMaterials();
	
	
		if ($model->load(\Yii::$app->request->post()) && $model->save()) {
			return $this->redirect('material');
		} else {
			return $this->render('creatematerial', [
					'model' => $model,
					'materials'=>$materials,
					'stu_id'=>$stu_id,
					'stu_name'=>$stu_name
			]);
		}
	}
	/**
	 * 修改物资申请
	 * @param unknown $id
	 * @return \yii\web\Response|string
	 */
	public function actionMaterialupdate($id)
	{
	    if (Check::check ()) {
        } else {
            return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
        }

		$model = MaterialborrowModel::findOne($id);
		$materials=new MaterialModel();
		$materials=$materials->getAllMaterials();
		$stu_id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
		$stu_name=\Yii::$app->session->get ( 'stu' ) ['stuname'];
		if ($model->load(\Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['viewmaterial', 'id' => $model->id]);
		} else {
			return $this->render('materialupdate', [
					'model' => $model,
					'materials'=>$materials,
					'stu_id'=>$stu_id,
    				'stu_name'=>$stu_name
			]);
		}
	}
	/**
	 * 学生-物资借用-详情页
	 * @param unknown $id
	 * @return string
	 */
	public function actionViewmaterial($id){
        if (Check::check ()) {
        } else {
            return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
        }
		$model=MaterialborrowModel::findOne($id);
		return $this->render('viewmaterial', [
				'model' => $model,
		]);
	}
	
	public function actionIndex(){
		
	}
	
	
}